package com.fingard.dsp.bank.directbank.jsjnrcb02;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;

public class JSJNRCB02Base extends DirectBase implements ITcpClient {
    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] tmpStrRet = new String[] { "", "" };

        WriteBankLogLn("2.提交银行的地址：" + bankFront.serverIP + ":" + bankFront.serverPort);
        WriteBankLogStep2(p_reqStr);

        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        String tmpCharset = getCharset();
        TcpReceiver tmpReceiver = tmpClient.sendMessage(p_reqStr.getBytes());
        tmpStrRet[0] = tmpReceiver.connState;
        if (tmpReceiver.isCompleted) {
            tmpStrRet[1] = new String(tmpReceiver.recBytes, tmpReceiver.contentStartIndex, tmpReceiver.contentLength,
                    tmpCharset);
            WriteBankLogStep3(tmpStrRet[1]);
        } else {
            tmpStrRet[1] = tmpReceiver.getConnMsg();
            WriteBankLogLn(tmpStrRet[1]);
        }
        return tmpStrRet;
    }

    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 0) {
                p_receiver.contentLength = p_receiver.readedLength;
                p_receiver.contentStartIndex = 0;
            }
        }
    }
}